package com.alibaba.algo.controller.dict;

import com.alibaba.algo.annotation.OperateLogAnnotation;
import com.alibaba.algo.apiimpl.dict.SysDictServiceImpl;
import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.common.SysConstant;
import com.alibaba.algo.cooperation.usercomponent.service.dict.SysDictCodeEnum;
import com.alibaba.algo.cooperation.usercomponent.vo.req.dict.CreateDictRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.dict.DeleteDictRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.dict.SelectDictListRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.dict.UpdateDictRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.dict.SysDictResponseVO;
import com.alibaba.algo.enums.OperateTypeEnum;
import com.alibaba.algo.enums.ResourceTypeEnum;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典
 */
@RestController
@RequestMapping(value = "/usercenter-admin-app/admin/sysDict")
public class SysDictController {

    @Resource
    private SysDictServiceImpl sysDictService;

    @PostMapping("/insert")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.DICT,operateType = OperateTypeEnum.INSERT)
    public BaseResult insert(@RequestBody CreateDictRequestVO param) {
        if (StringUtils.isEmpty(param.getCode())) {
            return BaseResult.buildError(300, "code不能为空");
        }
        if (StringUtils.isEmpty(param.getName())) {
            return BaseResult.buildError(300, "name不能为空");
        }
        return sysDictService.insert(param);
    }

    @PostMapping("/delete")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.DICT,operateType = OperateTypeEnum.DELETE)
    public BaseResult delete(@RequestBody DeleteDictRequestVO param) {
        return sysDictService.delete(param);
    }

    @PostMapping("/update")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.DICT,operateType = OperateTypeEnum.UPDATE)
    public BaseResult update(@RequestBody UpdateDictRequestVO param) {
        if (StringUtils.isEmpty(param.getId())) {
            return BaseResult.buildError(300, "id不能为空");
        }
        if (StringUtils.isEmpty(param.getCode())) {
            return BaseResult.buildError(300, "code不能为空");
        }
        if (StringUtils.isEmpty(param.getName())) {
            return BaseResult.buildError(300, "name不能为空");
        }
        return sysDictService.update(param);
    }

    @PostMapping("/selectPage")
    public BaseResult selectPage(@RequestBody SelectDictListRequestVO param) {
        if (ObjectUtils.isEmpty(param.getPageIndex())) {
            param.setPageIndex(1);
        }
        if (ObjectUtils.isEmpty(param.getPageSize())) {
            param.setPageSize(10);
        }
        return sysDictService.selectPage(param);
    }

    @PostMapping("/selectList")
    public BaseResult selectList(@RequestBody SelectDictListRequestVO param) {
        return sysDictService.selectList(param);
    }

    /**
     * 查询页面上适用的一些数据字典，比如浏览器标题，系统名称等
     */
    @PostMapping("/selectPageDict")
    public BaseResult selectPageDict() {
        SelectDictListRequestVO param = new SelectDictListRequestVO();
        param.setParentCode(SysDictCodeEnum.PAGE_DICT.getCode());
        BaseResult<List<SysDictResponseVO>> result = sysDictService.selectList(param);
        if (result.getCode().equals(SysConstant.OK)) {
            List<SysDictResponseVO> data = result.getData();
            if (data == null) {
                data = new ArrayList<>();
            }
            out:
            for (SysDictCodeEnum dict : SysDictCodeEnum.values()) {
                for (SysDictResponseVO temp : data) {
                    if (temp.getCode().equals(dict.getCode())) {
                        continue out;
                    }
                }
                SysDictResponseVO temp = new SysDictResponseVO();
                temp.setCode(dict.getCode());
                temp.setParentCode(dict.getParentCode());
                temp.setDesc(dict.getDesc());
                temp.setName(dict.getName());
                temp.setSort(dict.getSort());
                data.add(temp);
            }
        }
        return result;
    }

}
